//新建下载按钮
document.getElementById('add_download').addEventListener('click', function () {
    location.href = "add_download.html"
});


//保存配置文件
document.getElementById("setting_save").addEventListener('click', function () {
    setting_save();
});

//配置文件保存方法
function setting_save() {
    let setting_server = document.getElementById("setting_server").value;
    let setting_token = document.getElementById("setting_token").value;
    superagent
        .post(setting_server + '/status')
        .send({}) // 发送JSON POST主体
        .set('token', setting_token)
        .set('version', 'web')
        .end(function (err, res) {
            if (err) {
                foxui.message({
                    type: 'danger', text: '服务器地址不通，请检查'
                });
                return;
            }
            if (res.body.status===1) {
                setConfig({token: setting_token, server: setting_server});
                foxui.message({type: 'success', text: '保存成功'});
                location.reload()
            }else {
                foxui.message({type: 'danger', text: res.body.msg});
            }

        });



}

//加载配置文件
function setting_reload() {
    // chrome.storage.local.get(["token", "server"]).then((result) => {
    getConfig(["token", "server"],(result) => {
        if (result.server) {
            document.getElementById("setting_server").value = result.server;

            //刷新进度
            statusTask();
            setInterval(statusTask, 1000);

            //刷新完成
            historyTask();
            setInterval(historyTask, 5000);
        }
        if (result.token) {
            document.getElementById("setting_token").value = result.token;
        }
    });
}

//插件启动加载配置文件
setting_reload();


//获取正在下载的
function statusTask() {
    http("/statusTask", null, function (res) {

        let elementById = document.getElementById("statusTask");
        if (res.status) {

            elementById.innerHTML = "";
            let data = res.data;
            for (let i = 0; i < data.length; i++) {
                let datum = data[i];

                let speed = "";
                let status = "";
                if (datum.status == 1) {
                    speed = datum.statusTitle;
                    status = "foxui-icon-shuangxiangyou-o";
                }else if (datum.status == 2) {
                    status = "foxui-icon-zuiyou-o";
                    speed = datum.speed;
                }else if (datum.status == 3) {
                    speed = datum.statusTitle;
                    status = "foxui-icon-shuangxiangyou-o";

                }

                elementById.insertAdjacentHTML('afterbegin',
                    "<li class=\"foxui-table-tr\">\n" +
                    "                        <div class=\"foxui-table-td\" style=\"\">\n" +
                    "                            <img class=\"foxui-img-cover\" style=\"width: 20px;margin-right: 10px;\" src=\""+datum.icon+"\">\n" +
                    "                            <div>\n" +
                    "                                <span>"+datum.name+"</span>\n" +
                    "                                <span style=\"font-size:10px;\">("+datum.size+")</span>\n" +
                    "                                <div id=\""+datum.id+"_progress\" style=\"margin-right: 10px;width: 400px\"></div>\n" +
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                        <div class=\"foxui-table-td\" style=\"width: 200px;flex: none;\">\n" +
                    "                            <div class=\"foxui-button-group\">\n" +
                    "                                <button class=\"foxui-solid-primary foxui-size-mini foxui-shape-round\"> "+speed+"</button>\n" +
                    "                                <button id=\""+datum.id+"_status\" class=\"foxui-solid-primary foxui-size-mini foxui-shape-round\"><i class=\""+status+"\"></i></button>\n" +
                    "                                <button id=\""+datum.id+"_del\" class=\"foxui-solid-primary foxui-size-mini foxui-shape-round\"><i class=\"foxui-icon-shanchu-o\"></i></button>\n" +
                    "                            </div>\n" +
                    "                        </div>\n" +
                    " </li>");

                foxui.progress({
                    el: '#'+datum.id+'_progress',
                    percent: datum.progress,
                });


                //暂停按钮功能
                document.getElementById(datum.id+"_status").addEventListener('click', function () {
                    if (datum.status == 2) {//运行
                        stopTask(datum.id)
                    }else{//非运行
                        startTask(datum.id)
                    }
                });
                //删除按钮功能
                document.getElementById(datum.id+"_del").addEventListener('click', function () {
                    delTask(datum.id)
                });
            }
            if (data.length == 0) {
                elementById.insertAdjacentHTML("beforeend","没有更多的数据");
            }
        }

    });
};

//获取下载历史
function historyTask() {
    http("/historyTask", null, function (res) {
        let elementById = document.getElementById("historyTask");
        if (res.status) {
            elementById.innerHTML = "";
            let data = res.data;
            for (let i = 0; i < data.length; i++) {
                let datum = data[i];
                elementById.insertAdjacentHTML("afterbegin",
                    "                    <li class=\"foxui-table-tr\">\n" +
                    "                        <div class=\"foxui-table-td\" style=\"\">\n" +
                    "                            <img class=\"foxui-img-cover\" style=\"width: 20px;margin-right: 10px;\" src=\""+datum.icon+"\">\n" +
                    "                            <div>\n" +
                    "                                <span>"+datum.name+"</span>\n" +
                    "                                <span style=\"font-size:10px;\">("+datum.time+")</span>\n" +
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                        <div class=\"foxui-table-td\" style=\"width: 150px;flex: none;\">\n" +
                    "                            <div class=\"foxui-button-group\">\n" +
                    "                                <button class=\"foxui-solid-primary foxui-size-mini foxui-shape-round\" style='width: 80px'>"+datum.size+"</button>\n" +
                    "                                <button id=\""+datum.id+"_del\" class=\"foxui-solid-primary foxui-size-mini foxui-shape-round\"><i class=\"foxui-icon-shanchu-o\"></i></button>\n" +
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                    </li>");

                //删除按钮功能
                document.getElementById(datum.id+"_del").addEventListener('click', function () {
                    delTask(datum.id)
                });
            }
            if (data.length == 0) {
                elementById.insertAdjacentHTML("beforeend","没有更多的数据");
            }

        }
    });
};

//开始下载
function startTask(id){
    foxui.loading();
    http("/startTask", {
        id:id
    },function (res) {
        setTimeout(() => {
            foxui.closeLoading();
        }, 1000);

    },{
        http(){ setTimeout(() => {
            foxui.closeLoading();
        }, 1000);},
        body(){ setTimeout(() => {
            foxui.closeLoading();
        }, 1000);}
    });
}

//暂停下载
function stopTask(id){
    foxui.loading();
    http("/stopTask", {
        id:id
    },function (res) {
        setTimeout(() => {
            foxui.closeLoading();
        }, 1000);

    },{
        http(){setTimeout(() => {
            foxui.closeLoading();
        }, 1000);},
        body(){ setTimeout(() => {
            foxui.closeLoading();
        }, 1000);}
    });
}

//删除下载
function delTask(id){
    foxui.loading();
    http("/delTask", {
        id:id
    },function (res) {
        setTimeout(() => {
            foxui.closeLoading();
        }, 1000);

    },{
        http(){
            setTimeout(() => {
                foxui.closeLoading();
            }, 1000);
        },
        body(){
            setTimeout(() => {
                foxui.closeLoading();
            }, 1000);
        }
    });
}


//检查更新
getVersion(function (version) {
    setConfig({version:version})
})

if (location.href.indexOf("http://") !== -1) {
    document.getElementById("download_browser_plugin").style.display="block";
}

if (location.href.indexOf("https://") !== -1) {
    document.getElementById("download_browser_plugin").style.display="block";
}
